﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
using System.IO;
public partial class Manage_Emp3_ClassEmp3Detail : AdminPageBase
{
    int id;
    int classtypeid = 0;
	private string returnURL;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        GetClassEmpId();
		returnURL = Request.QueryString["returnURL"].ToString();
        if (!IsPostBack)
        {
            // 正确的属性设置方法
            this.GridView1.Attributes.Add("SortExpression", "PlanFinishId");
            this.GridView1.Attributes.Add("SortDirection", "ASC");
            this.GridView2.Attributes.Add("SortExpression", "EmpId");
            this.GridView2.Attributes.Add("SortDirection", "ASC");
            DataTable dt = new DataTable();
            dt.Columns.Add("EmpId");
            dt.Columns.Add("CenterName");
            dt.Columns.Add("CityName");
            dt.Columns.Add("DeptName");
            dt.Columns.Add("StoreCode");
            dt.Columns.Add("Name");
            dt.Columns.Add("Code");
            dt.Columns.Add("JobName");
           
            dt.Columns.Add("BrandName");
          
            dt.Columns.Add("");

            GridviewControl.GridViewDataBind(this.GridView2, dt);
            GridviewControl.ResetGridView(this.GridView2);
            BindClassType();
            BindClassEmp();
            BindClass();
            BindPlanFinish();
        }
    }
    protected void BindClassType()
    {

        DataSet ds = jtDataAccess.SearchClassType("", 10, 0, "Name", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlclassType.DataSource = ds;
            ddlclassType.DataTextField = "Name";
            ddlclassType.DataValueField = "ClassTypeId";
            ddlclassType.DataBind();
        }
        ListItem item = new ListItem("选择课程类型", "0");
        ddlclassType.Items.Insert(0, item);

    }
    protected void BindClass()
    {
        classtypeid = Convert.ToInt32(ddlclassType.SelectedValue);

        DataSet ds = jtDataAccess.GetList("Class", "ClassTypeId=" + classtypeid, "Name");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlClass.DataSource = ds;
            ddlClass.DataTextField = "Name";
            ddlClass.DataValueField = "ClassId";
            ddlClass.DataBind();
        }
        ListItem item = new ListItem("选择课程", "0");
        ddlClass.Items.Insert(0, item);

    }
    protected void GetClassEmpId()
    {
        if (Request.QueryString["id"] == null)
        {
            id = 0;

        }
        else
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
            hdfClassEmpId.Value = id.ToString();
        }
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }
    }
    protected void BindClassEmp()
    {
        if (id != 0)
        {
            DataSet ds = jtDataAccess.GetClassEmp(id);
            if (CoreTools.CheckDataSet(ds))
            {
                lblNum.Text = ds.Tables[0].Rows[0]["Num"].ToString();
                ddlClass.SelectedValue = ds.Tables[0].Rows[0]["ClassId"].ToString();
                ddlclassType.SelectedValue = ds.Tables[0].Rows[0]["ClassTypeId"].ToString();
                lblClassCode.Text = ds.Tables[0].Rows[0]["ClassCode"].ToString();
                lblclassName.Text = ds.Tables[0].Rows[0]["ClassName"].ToString();
                hdfClassId.Value = ds.Tables[0].Rows[0]["ClassId"].ToString();
                txttitle.Text = ds.Tables[0].Rows[0]["Title"].ToString();

            }

        }
        else
        {
            DataSet ds = jtDataAccess.GetNewNum("ClassEmp1");
            if (CoreTools.CheckDataSet(ds))
                lblNum.Text = ds.Tables[0].Rows[0]["Num"].ToString();
            ddlclassType.SelectedValue = "0";
            ddlClass.DataSource = null;
            ddlClass.DataBind();
            lblClassCode.Text = "";
            lblclassName.Text = "";
        }
    }
    protected void BindPlanFinish()
    {
        int classEmpId = Convert.ToInt32(hdfClassEmpId.Value);
        if (classEmpId != 0)
        {
            DataSet dsemp = jtDataAccess.GetPlanFinishByClassEmpId(classEmpId,0);
            if (CoreTools.CheckDataSet(dsemp))
            {
                // 获取GridView排序数据列及排序方向
                string sortExpression = this.GridView1.Attributes["SortExpression"];
                string sortDirection = this.GridView1.Attributes["SortDirection"];

                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dsemp.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据
               // this.GridView1.DataSource = dsemp.Tables[0];
              //  this.GridView1.DataBind();
                GridviewControl.GridViewDataBind(this.GridView1, dsemp.Tables[0]);
                Button1.Enabled = true;
                selectAll1.Enabled = true;
                ibtnsearch.Enabled = true;
            }
            else
            {
                //this.GridView1.DataSource = null;
               // this.GridView1.DataBind();
                DataTable dt = new DataTable();
                dt.Columns.Add("PlanFinishId");
                dt.Columns.Add("CenterName");
                dt.Columns.Add("CityName");
                dt.Columns.Add("DeptName");
                dt.Columns.Add("StoreCode");
                dt.Columns.Add("Name");
                dt.Columns.Add("Code");
                dt.Columns.Add("JobName");
                dt.Columns.Add("EntryDate", typeof(DateTime));
                dt.Columns.Add("BrandName");
                dt.Columns.Add("Period");
                dt.Columns.Add("EmpId");
                dt.Columns.Add("Finish");
                dt.Columns.Add("FinishId");
                dt.Columns.Add("");

                GridviewControl.GridViewDataBind(this.GridView1, dt);
                GridviewControl.ResetGridView(this.GridView1);
                Button1.Enabled = false;
                selectAll1.Enabled = false;
            }
			
        }
       else
        {
            GridviewControl.ResetGridView(this.GridView1);

        }
    }
    protected void btnAddEmp_Click(object sender, EventArgs e)
    {
        int classEmpId = Convert.ToInt32(hdfClassEmpId.Value);
        if (classEmpId == 0)
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请先保存上课课程信息！"), false);
        }
        else
        {
            int flag = 0;
            flag = SaveClassEmp();
            if (flag > 0)
            {
                divadd_emp.Visible = true;
                DataSet ds = jtDataAccess.SearchCenter("", 10, 0, "Name", "Asc");
                if (CoreTools.CheckDataSet(ds))
                {
                    ddlcenter.DataSource = ds;
                    ddlcenter.DataTextField = "Name";
                    ddlcenter.DataValueField = "CenterId";
                    ddlcenter.DataBind();
                }
                ListItem item = new ListItem("选择指挥中心", "0");
                ddlcenter.Items.Insert(0, item);

                DataSet dsjob = jtDataAccess.SearchJob("",10,0,"Name","Asc");
                if (CoreTools.CheckDataSet(dsjob))
                {
                    ddljob.DataSource = dsjob;
                    ddljob.DataTextField = "Name";
                    ddljob.DataValueField = "JobId";
                    ddljob.DataBind();
                }
                ListItem item1 = new ListItem("选择职位", "0");
                ddljob.Items.Insert(0, item1);

                DataTable dt = new DataTable();
                dt.Columns.Add("EmpId");
                dt.Columns.Add("CenterName");
                dt.Columns.Add("CityName");
                dt.Columns.Add("DeptName");
                dt.Columns.Add("StoreCode");
                dt.Columns.Add("Name");
                dt.Columns.Add("Code");
                dt.Columns.Add("JobName");

                dt.Columns.Add("BrandName");

                dt.Columns.Add("");

                GridviewControl.GridViewDataBind(this.GridView2, dt);
              
            }
        }
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        int flag = SaveClassEmp();
        switch (flag)
        {
            case -1:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，已经存在相同编号的课程！"), false);
                break;
            case 0:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败！"), false);
                break;
            default:
                id = flag;
                hdfClassEmpId.Value = flag.ToString();
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                break;
        }
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindPlanFinish();
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            Label lblEntryDate = (Label)e.Row.FindControl("lblEntryDate");
            e.Row.Cells[4].Attributes.Add("class", "text");

            e.Row.Cells[6].Attributes.Add("class", "text");
            Button btnfinish = (Button)e.Row.FindControl("btnfinish");
            HiddenField hdfIsFininsh = (HiddenField)e.Row.FindControl("hdfIsFininsh1");
            if (hdfIsFininsh != null)
            {
                if (btnfinish != null && hdfIsFininsh.Value == "0")
                     btnfinish.Visible = true;
                    
                else if (btnfinish != null)
                    btnfinish.Visible = false;
                   
            }
          
            if (lblEntryDate != null && lblEntryDate.Text != "")
            {
                lblEntryDate.Text = Convert.ToDateTime(lblEntryDate.Text).ToShortDateString().Replace("1900/1/1", "");
            }

            LinkButton BtnDelete = (LinkButton)e.Row.Cells[13].Controls[2];
            if (BtnDelete.Text == "删除")
            {
                ((LinkButton)e.Row.Cells[13].Controls[2]).Attributes.Add("onclick", "javascript:return confirm('您确定要删除该记录吗？')");
            }
        }

        if (edit == 1)
            ((CommandField)GridView1.Columns[13]).ShowEditButton = true;
        else
            ((CommandField)GridView1.Columns[13]).ShowEditButton = false;
        if (delete == 1)
            ((CommandField)GridView1.Columns[13]).ShowDeleteButton = true;
        else
            ((CommandField)GridView1.Columns[13]).ShowDeleteButton = false;

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        UserType ut = new UserType();//用户自定义类型
        DataTable PlanFinishTable = ut.GetPlanFinishTableType();

        DataRow PlanFinishTableRow;
        int PlanFinishId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());


        PlanFinishTableRow = PlanFinishTable.NewRow();
        PlanFinishTableRow["PlanFinishId"] = PlanFinishId;
        PlanFinishTable.Rows.Add(PlanFinishTableRow);

        PlanFinishTable.AcceptChanges();
        int flag = jtDataAccess.OpPlanFinish(JTDataAccess.OpType.Delete, PlanFinishTable,0);
        switch (flag)
        {
            case 0:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除失败！"), false);
                break;

            case 1:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除成功！"), false);
                break;
            default:
                break;
        }

        GridView1.EditIndex = -1;
        BindPlanFinish();
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindPlanFinish();
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        int flag = 0;
        int PlanFinishId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        int classid = Convert.ToInt32(hdfClassId.Value);
        int empid = Convert.ToInt32(((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfEmpId")).Value);
        int IsFinish = Convert.ToInt32(((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfIsFininsh")).Value);
        TextBox cPeriod = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPeriod");
        string Period = "";
        if (cPeriod.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写计划安排时间！"), false);
            cPeriod.Focus();
        }
        else if (!Regex.IsMatch(cPeriod.Text.ToString(), "^([2-9]\\d{3})(0[1-9]|1[0-2])$"))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("计划安排时间格式不正确，正确格式形如：201206！"), false);
            cPeriod.Focus();
        }
        else
            Period = cPeriod.Text.Trim();
        if (Period != "" && !CoreTools.ProcessSqlStr(Period, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
        }
        if (Period != "")
        {
            UserType ut = new UserType();//用户自定义类型
            DataTable PlanFinishTable = ut.GetPlanFinishTableType();
            DataRow PlanFinishTableRow;
            PlanFinishTableRow = PlanFinishTable.NewRow();
            PlanFinishTableRow["PlanFinishId"] = PlanFinishId;
            PlanFinishTableRow["ClassEmpId"] = id;
            PlanFinishTableRow["EmpId"] = empid;
            PlanFinishTableRow["ClassId"] = classid;
            PlanFinishTableRow["Period"] = Period;

            PlanFinishTable.Rows.Add(PlanFinishTableRow);
            flag = jtDataAccess.OpPlanFinish(JTDataAccess.OpType.Update, PlanFinishTable, IsFinish);
            switch (flag)
            {
                case 1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，请重试！"), false);
                    break;
                default:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);

                    break;
            }
            GridView1.EditIndex = -1;
            BindPlanFinish();
        }
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView1.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;

        this.BindPlanFinish();
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void btnMassAdd_Click(object sender, EventArgs e)
    {
        string period = txtPeriod.Text.Trim();
        int classid = Convert.ToInt32(hdfClassId.Value);
        int classempid = Convert.ToInt32(hdfClassEmpId.Value);
        if (classempid == 0)
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("必须先保存上课主体信息！"), false);
        }
        else if (classid == 0)
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请选择一课程，并先保存上课主体信息！"), false);
        }
        else
        {
            if (period != "" && !CoreTools.ProcessSqlStr(period, 0))
            {
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            }
            else
            {
                UserType ut = new UserType();//用户自定义类型
                DataTable PlanFinishTable = ut.GetPlanFinishTableType();
                int count = 0;
                DataRow PlanFinishTableRow;
                for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
                {
                    CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("chkID");
                    HiddenField hdfEmpId = (HiddenField)GridView2.Rows[i].FindControl("hdfEmpId");

                    if (cbox.Checked == true && hdfEmpId != null && hdfEmpId.Value != "")
                    {
                        count++;
                        PlanFinishTableRow = PlanFinishTable.NewRow();
                        PlanFinishTableRow["ClassEmpId"] = classempid;
                        PlanFinishTableRow["ClassId"] = classid;
                        PlanFinishTableRow["EmpId"] = Convert.ToInt32(hdfEmpId.Value);
                        PlanFinishTableRow["Period"] = period;
                        PlanFinishTable.Rows.Add(PlanFinishTableRow);
                    }
                }
                if (count == 0)
                {
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("您还未选择任何一项！"), false);
                }
                else
                {
                    PlanFinishTable.AcceptChanges();
                    int flag = jtDataAccess.OpPlanFinish(JTDataAccess.OpType.Insert, PlanFinishTable,0);

                    switch (flag)
                    {
                        case 0:
                            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("添加失败！"), false);
                            break;

                        case 1:
                            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("添加成功！"), false);
                            BindPlanFinish();
                            BindSelectEmp();
                            break;

                        default:
                            break;
                    }
                    selectAll.Checked = false;
                }
            }
        }
        GridviewControl.ResetGridView(this.GridView1);
    }
    protected void selectAll_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("chkID");
            if (selectAll.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
        GridviewControl.ResetGridView(this.GridView1);
    }
    protected void btnDeleteMass_Click(object sender, EventArgs e)
    {
        UserType ut = new UserType();//用户自定义类型
        DataTable PlanFinishTable = ut.GetPlanFinishTableType();
        int count = 0;
        DataRow PlanFinishTableRow;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("chkID1");
            HiddenField hdfPlanFinishId = (HiddenField)GridView1.Rows[i].FindControl("hdfPlanFinishId");
            if (cbox.Checked == true && hdfPlanFinishId != null && hdfPlanFinishId.Value != "")
            {
                count++;
                PlanFinishTableRow = PlanFinishTable.NewRow();
                PlanFinishTableRow["PlanFinishId"] = hdfPlanFinishId.Value;
                PlanFinishTable.Rows.Add(PlanFinishTableRow);
            }
        }
        if (count == 0)
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("您还未选择任何一项！"), false);
        }
        else
        {
            PlanFinishTable.AcceptChanges();
            int flag = jtDataAccess.OpPlanFinish(JTDataAccess.OpType.Delete, PlanFinishTable,0);

            switch (flag)
            {
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除失败！"), false);
                    break;

                case 1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除成功！"), false);
                    BindPlanFinish();
                    break;

                default:
                    break;
            }
            selectAll1.Checked = false;
        }
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void selectAll1_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("chkID1");
            if (selectAll1.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void ibtnsearch_Click(object sender, ImageClickEventArgs e)
    {
        string keyword = txtkeyword.Text.Trim();
        if (!CoreTools.ProcessSqlStr(keyword, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
        }
        else
        {
            DataSet ds = jtDataAccess.GetClass(2, keyword);
            if (CoreTools.CheckDataSet(ds))
            {
                lblClassCode.Text = ds.Tables[0].Rows[0]["Code"].ToString();
                lblclassName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                hdfClassId.Value = ds.Tables[0].Rows[0]["ClassId"].ToString();
            }
        }
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

        }
    }
    protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView2.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView2.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView2.Attributes["SortExpression"] = sortExpression;
        this.GridView2.Attributes["SortDirection"] = sortDirection;

        this.BindSelectEmp();
        GridviewControl.ResetGridView(this.GridView1);
    }

    protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        hdfClassId.Value = ddlClass.SelectedValue.ToString();
        DataSet ds = jtDataAccess.GetList("Class", "ClassId=" + ddlClass.SelectedValue.ToString(), "Name");
        if (CoreTools.CheckDataSet(ds))
        {
            lblClassCode.Text = ds.Tables[0].Rows[0]["Code"].ToString();
            lblclassName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
        }
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void ddlclassType_SelectedIndexChanged(object sender, EventArgs e)
    {
        classtypeid = Convert.ToInt32(ddlclassType.SelectedValue);
        DataSet ds = jtDataAccess.GetClass(1, classtypeid.ToString());
        if (CoreTools.CheckDataSet(ds))
        {
            ddlClass.DataSource = null;
            ddlClass.DataBind();
            ddlClass.DataSource = ds;
            ddlClass.DataTextField = "Name";
            ddlClass.DataValueField = "ClassId";
            ddlClass.DataBind();
        }
        ListItem item = new ListItem("选择课程", "0");
        ddlClass.Items.Insert(0, item);
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void btnreturn_Click(object sender, EventArgs e)
    {
       // Response.Redirect("List3.aspx?MenuId=" + MenuId);
	    Response.Redirect(returnURL);
    }
    protected int SaveClassEmp()
    {
        int flag = 0;
        string Num = lblNum.Text;
        int classid = Convert.ToInt32(hdfClassId.Value);
        int EmpTypeId = 3;
        string title = txttitle.Text.Trim();
        if (!CoreTools.ProcessSqlStr(title, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txttitle.Focus();
        }
        else
        {
            int classempid = Convert.ToInt32(hdfClassEmpId.Value);
            if (classempid == 0)
                flag = jtDataAccess.OpClassEmp(JTDataAccess.OpType.Insert, 0, EmpTypeId, classid, Num, title, int.Parse(AdminSessionHelper.UserDetails["UserId"].ToString()));
            else
                flag = jtDataAccess.OpClassEmp(JTDataAccess.OpType.Update, classempid, EmpTypeId, classid, Num, title, int.Parse(AdminSessionHelper.UserDetails["UserId"].ToString()));

        }
        return flag;

    }
    protected void BindSelectEmp()
    {
        int classid = Convert.ToInt32(hdfClassId.Value);
        int centerid = Convert.ToInt32(ddlcenter.SelectedValue);
        int cityid = Convert.ToInt32(ddlcity.SelectedValue);
        int jobid =Convert.ToInt32(ddljob.SelectedValue);
        int emptypeId = 3;
        string keyword = txtkey.Text.Trim();
        if (!CoreTools.ProcessSqlStr(keyword, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
        }
        else if (classid != 0 && centerid != 0)
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.GridView2.Attributes["SortExpression"];
            string sortDirection = this.GridView2.Attributes["SortDirection"];
            DataSet ds = jtDataAccess.FilterEmp(emptypeId,classid, centerid, cityid,jobid,keyword);
            if (CoreTools.CheckDataSet(ds))
            {
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据
               // this.GridView2.DataSource = ds.Tables[0];
              //  this.GridView2.DataBind();
                GridviewControl.GridViewDataBind(this.GridView2, ds.Tables[0]);
                // pager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["Total"]);
                btnMassAdd.Enabled = true;
                selectAll.Enabled = true;

            }
            else
            {
                //this.GridView2.DataSource = null;
                //this.GridView2.DataBind();
                // pager1.RecordCount = 0;
                DataTable dt = new DataTable();
                dt.Columns.Add("EmpId");
                dt.Columns.Add("CenterName");
                dt.Columns.Add("CityName");
                dt.Columns.Add("DeptName");
                dt.Columns.Add("StoreCode");
                dt.Columns.Add("Name");
                dt.Columns.Add("Code");
                dt.Columns.Add("JobName");

                dt.Columns.Add("BrandName");

                dt.Columns.Add("");

                GridviewControl.GridViewDataBind(this.GridView2, dt);
                btnMassAdd.Enabled = false;
                selectAll.Enabled = false;

            }
        }
        else
        {
            GridviewControl.ResetGridView(this.GridView2);
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        BindSelectEmp();
        GridviewControl.ResetGridView(this.GridView1);
    }
    protected void ddlcenter_SelectedIndexChanged(object sender, EventArgs e)
    {
        int centerid = Convert.ToInt32(ddlcenter.SelectedValue);
        ddlcity.Items.Clear();
        DataSet ds = jtDataAccess.GetList("City","CenterId="+centerid,"Name");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlcity.DataSource = ds;
            ddlcity.DataTextField = "Name";
            ddlcity.DataValueField = "CityId";
            ddlcity.DataBind();
        }
        ListItem item = new ListItem("请选择一个城市","0");
        ddlcity.Items.Insert(0,item);
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void btnexport_Click(object sender, EventArgs e)
    {
        if (GridView1.EditIndex != -1)
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("还有项处于编辑中，请完成编辑再导出！"), false);
        else
        {
            DataTable dt = GetExportData();
            if (dt.Rows.Count == 0)
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("没有数据，或没有选择要导出的行！"), false);
            else
            // ExcelExport(GetStringWriter(dt), "课程.xls");
            {
                string style = @"<style> .text { mso-number-format:\@;color:#000000 } </style> ";

                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.Charset = "GB2312";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("上课学员名单.xls", System.Text.Encoding.GetEncoding("utf-8")));
                HttpContext.Current.Response.ContentType = "application/ms-excel";

                StringWriter sw = new StringWriter();

                HtmlTextWriter htw = new HtmlTextWriter(sw);
                GridView1.Columns[0].Visible = false;
                GridView1.Columns[12].Visible = false;
				 GridView1.Columns[13].Visible = false;
                GridView1.AllowPaging = false;
                GridView1.AllowSorting = false;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                GridView1.RenderControl(htw);

                // Style is added dynamically
                Response.Write(style);

                Response.Write(sw.ToString());

                Response.End();
            }
        }
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    protected DataTable GetExportData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PlanFinishId");
        dt.Columns.Add("CenterName");
        dt.Columns.Add("CityName");
        dt.Columns.Add("DeptName");
        dt.Columns.Add("StoreCode");
        dt.Columns.Add("Name");
        dt.Columns.Add("Code");
        dt.Columns.Add("JobName");
        dt.Columns.Add("EntryDate", typeof(DateTime));
        dt.Columns.Add("BrandName");
        dt.Columns.Add("Period");
        dt.Columns.Add("EmpId");
        dt.Columns.Add("Finish");
        dt.Columns.Add("FinishId");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkID1 = (CheckBox)GridView1.Rows[i].FindControl("chkID1");
            if (chkID1 != null && chkID1.Checked == true)
            {
                HiddenField hdfPlanFinishId = (HiddenField)GridView1.Rows[i].FindControl("hdfPlanFinishId");
                HiddenField hdfEmpId1 = (HiddenField)GridView1.Rows[i].FindControl("hdfEmpId1");
                Label lblCenterName = (Label)GridView1.Rows[i].FindControl("lblCenterName");
                Label lblCityName = (Label)GridView1.Rows[i].FindControl("lblCityName");
                Label lblDeptName = (Label)GridView1.Rows[i].FindControl("lblDeptName");
                Label lblStoreCode = (Label)GridView1.Rows[i].FindControl("lblStoreCode");
                Label lblName = (Label)GridView1.Rows[i].FindControl("lblName");
                Label lblCode = (Label)GridView1.Rows[i].FindControl("lblCode");
                Label lblJobName = (Label)GridView1.Rows[i].FindControl("lblJobName");
                Label lblBrandName = (Label)GridView1.Rows[i].FindControl("lblBrandName");
                Label lblEntryDate = (Label)GridView1.Rows[i].FindControl("lblEntryDate");
                Label lblPeriod = (Label)GridView1.Rows[i].FindControl("lblPeriod");
                Label lblIsFininsh = (Label)GridView1.Rows[i].FindControl("lblIsFininsh");
                HiddenField hdfIsFininsh = (HiddenField)GridView1.Rows[i].FindControl("hdfIsFininsh");
                DataRow row = dt.NewRow();
                if (hdfPlanFinishId != null)
                    row["PlanFinishId"] = hdfPlanFinishId.Value;
                if (hdfEmpId1 != null)
                    row["EmpId"] = hdfEmpId1.Value;
                if (lblCenterName != null)
                    row["CenterName"] = lblCenterName.Text;
                if (lblCityName != null)
                    row["CityName"] = lblCityName.Text;
                if (lblDeptName != null)
                    row["DeptName"] = lblDeptName.Text;
                if (lblName != null)
                    row["Name"] = lblName.Text;
                if (lblCode != null)
                    row["Code"] = lblCode.Text;
                if (lblStoreCode != null)
                    row["StoreCode"] = lblStoreCode.Text;
                if (lblJobName != null)
                    row["JobName"] = lblJobName.Text;
                if (lblBrandName != null)
                    row["BrandName"] = lblBrandName.Text;
                if (lblEntryDate != null)
                    row["EntryDate"] = DateTime.Parse(lblEntryDate.Text);
                if (lblPeriod != null)
                    row["Period"] = lblPeriod.Text;
                if (lblIsFininsh != null)
                    row["Finish"] = lblIsFininsh.Text;
                if (hdfIsFininsh != null)
                    row["FinishId"] = hdfIsFininsh.Value;
                dt.Rows.Add(row);

            }
        }
        dt.AcceptChanges();
        return dt;

    }

    protected void ckbIsHidFinishRecod_CheckedChanged(object sender, EventArgs e)
    {
        if (ckbIsHidFinishRecod.Checked == true)
        {
            BindPlanFinishNoFinished();
        }
        else
        {
            BindPlanFinish();
        }
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void BindPlanFinishNoFinished()
    {
        int classEmpId = Convert.ToInt32(hdfClassEmpId.Value);
        if (classEmpId != 0)
        {
            DataSet dsemp = jtDataAccess.GetPlanFinishByClassEmpId(classEmpId, 1);
            if (CoreTools.CheckDataSet(dsemp))
            {
                // 获取GridView排序数据列及排序方向
                string sortExpression = this.GridView1.Attributes["SortExpression"];
                string sortDirection = this.GridView1.Attributes["SortDirection"];

                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dsemp.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据
                // this.GridView1.DataSource = dsemp.Tables[0];
                //  this.GridView1.DataBind();
                GridviewControl.GridViewDataBind(this.GridView1, dsemp.Tables[0]);
                Button1.Enabled = true;
                selectAll1.Enabled = true;
                ibtnsearch.Enabled = true;
            }
            else
            {
                //this.GridView1.DataSource = null;
                // this.GridView1.DataBind();
                DataTable dt = new DataTable();
                dt.Columns.Add("PlanFinishId");
                dt.Columns.Add("CenterName");
                dt.Columns.Add("CityName");
                dt.Columns.Add("DeptName");
                dt.Columns.Add("StoreCode");
                dt.Columns.Add("Name");
                dt.Columns.Add("Code");
                dt.Columns.Add("JobName");
                dt.Columns.Add("EntryDate", typeof(DateTime));
                dt.Columns.Add("BrandName");
                dt.Columns.Add("Period");
                dt.Columns.Add("EmpId");
                dt.Columns.Add("Finish");
                dt.Columns.Add("");

                GridviewControl.GridViewDataBind(this.GridView1, dt);
                GridviewControl.ResetGridView(this.GridView1);
                Button1.Enabled = false;
                selectAll1.Enabled = false;
            }

        }
        else
        {
            GridviewControl.ResetGridView(this.GridView1);

        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("finish"))
        {
            GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
            int flag = 0;
            int PlanFinishId = Convert.ToInt32(e.CommandArgument.ToString());
            int classid = Convert.ToInt32(hdfClassId.Value);
            int empid = Convert.ToInt32(((HiddenField)drv.FindControl("hdfEmpId2")).Value);
            HiddenField hdfIsFinish1 = (HiddenField)drv.FindControl("hdfIsFininsh1");
            Button btnfinish = (Button)drv.FindControl("btnfinish");
            Label cperiod = (Label)drv.FindControl("lblPeriod");
            Label lblIsFininsh = (Label)drv.FindControl("lblIsFininsh");
            string Period = "";
            Period = cperiod.Text.Trim();
            if (Period != "" && !CoreTools.ProcessSqlStr(Period, 0))
            {
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            }
            if (Period != "")
            {
                UserType ut = new UserType();//用户自定义类型
                DataTable PlanFinishTable = ut.GetPlanFinishTableType();
                DataRow PlanFinishTableRow;
                PlanFinishTableRow = PlanFinishTable.NewRow();
                PlanFinishTableRow["PlanFinishId"] = PlanFinishId;
                PlanFinishTableRow["ClassEmpId"] = id;
                PlanFinishTableRow["EmpId"] = empid;
                PlanFinishTableRow["ClassId"] = classid;
                PlanFinishTableRow["Period"] = Period;

                PlanFinishTable.Rows.Add(PlanFinishTableRow);
                flag = jtDataAccess.OpPlanFinish(JTDataAccess.OpType.Update, PlanFinishTable, 1);
                switch (flag)
                {
                    case 1:
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                          btnfinish.Visible = false;
                    
                        hdfIsFinish1.Value = "1";
                        lblIsFininsh.Text = "完成";
                        break;
                    case 0:
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，请重试！"), false);
                        break;
                    default:
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，请重试！"), false);

                        break;
                }
               // GridView1.EditIndex = -1;
               // BindPlanFinish();
            }
           
        }
        GridviewControl.ResetGridView(this.GridView2);
    }
}